<?php namespace Admin\Controller;

use Common\Model\Goods_list;
use Common\Model\Goods;
	
	class Goods_listController extends  CommonController{
		private $db;
		private $goodsDb;
		
		public function __init(){
			parent::__init();
			
			$this->db =new Goods_list;
			$this->goodsDb =new Goods;
			
		}
		
		
		public function index(){
			
			if(IS_POST){
//				var_dump($_POST);die;
				$gid=Q('get.gid',0,'intval');
				$result=$this->db->store();
				if(!$result){
					View::error($this->db->getError());
				}
				
				View::success('添加成功',U('index',array('gid'=>$gid)));

			}
						
//			获取下拉列表的数据							
			$gid=Q('get.gid',0,'intval');
//			var_dump($gid);die;
			$groupData=Db::table('goods g')
			->join('goods_attr ga','g.gid','=','ga.goods_gid')
			->join('type_attr ta','ga.type_attr_taid','=','ta.taid')
			->field('gaid','ganame','type_attr_taid','goods_gid','taname')
			->where('gid',$gid)
			->where('class',1)
			->groupBy('type_attr_taid')
			->get();									
//			var_dump($groupData);die;
			
			$allData=Db::table('goods g')
			->join('goods_attr ga','g.gid','=','ga.goods_gid')
			->join('type_attr ta','ga.type_attr_taid','=','ta.taid')
			->field('gaid','ganame','type_attr_taid','goods_gid','taname')
			->where('gid',$gid)
			->where('class',1)
			->get();
			
//			$allData=current($allData);
//			var_dump($allData);die;

			
			View::with('groupData',$groupData);
			View::with('allData',$allData);
			

			foreach($allData as $kk=>$vv){
				$recom=array(
					$vv['gaid']=>$vv['ganame'],
				);	
					
			}			
						
			$goods_listData=$this->db->where('goods_gid',$gid)->get();
//			从数据库中提取对应的数据，并把combine字段拆分为color和size的序号
//			再把color和size插入数据库 分配到页面
//			var_dump($goods_listData);die;
			$Goods_attr_Model=new \Common\Model\Goods_attr;
			
			foreach($goods_listData as $k=>$v){
	//			将当前循环到的货品属性由字符串转换成数组
				$goods_listData[$k]['combine']=explode(',', $v['combine']);
	//			循环当前转换成数组的货品属性
	//				申请一个空数组
				
				$attrArr=array();
				foreach($goods_listData[$k]['combine'] as $kk=>$vv){
					$attr=$Goods_attr_Model->where('gaid',$vv)->pluck('ganame');
					$attrArr[$kk]=$attr;  
				};
	//			将经过循环获取和重组的数据压入商品货品数据中去
				$goods_listData[$k]['combine']=$attrArr;
			}				
			
//			var_dump($goods_listData);die;
			
			

			View::with('goods_listData',$goods_listData);
			
//			p($gldata);die;
						
			View::make();
		}
		
		
		
		public function del(){
			$glid=Q('get.glid',0,'intval');
			$gid=Q('get.gid',0,'intval');
			$this->db->where('glid',$glid)->delete();
			
			
			View::success('删除成功',U('index',array('gid'=>$gid)));
			
		}
		
		
		public function edit(){
			
			if(IS_POST){
				//				var_dump($_POST);die;
				$gid=Q('get.gid',0,'intval');
				$result=$this->db->edit();
				if(!$result){
					View::error($this->db->getError());
				}
				
				View::success('修改成功',U('index'));
				
				
			}			
			
			$gid=Q('get.gid',0,'intval');
			$glid=Q('get.glid',0,'intval');
			
			$thisData=$this->db->where('glid',$glid)->find();
			$thisData['combine']=explode(',', $thisData['combine']);
//			var_dump($thisData);die;
//			var_dump($gid);die;
			$groupData=Db::table('goods g')
			->join('goods_attr ga','g.gid','=','ga.goods_gid')
			->join('type_attr ta','ga.type_attr_taid','=','ta.taid')
			->field('gaid','ganame','type_attr_taid','goods_gid','taname')
			->where('gid',$gid)
			->where('class',1)
			->groupBy('type_attr_taid')
			->get();									
//			var_dump($groupData);die;
			
			$allData=Db::table('goods g')
			->join('goods_attr ga','g.gid','=','ga.goods_gid')
			->join('type_attr ta','ga.type_attr_taid','=','ta.taid')
			->field('gaid','ganame','type_attr_taid','goods_gid','taname')
			->where('gid',$gid)
			->where('class',1)
			->get();
			
//			$allData=current($allData);
//			var_dump($allData);die;

			View::with('thisData',$thisData);			
			View::with('groupData',$groupData);
			View::with('allData',$allData);
			
			View::make();
		}
		
		
		
		
		
		
		
		
		
	}
	
	
	
	
	
	
	
	
	?>